// pages/register/register.js
import { register,sms } from '../../../api.js'
var app = getApp(); //获取App内部的信息
Page({

  /* 页面的初始数据*/
  data: {
    sendcode:false,
    colora:false,
    colorb:false,
    colorc:false,
    isAuth: true, 
    phone: '',
    password: '',
    code:'',
    time:'发送验证码',//倒计时
    currentTime:61,
    disabled:false
  },

 
  onLoad: function (options) {
   
  },
  sendCode(){
    console.log(this.data.sendcode)
    if (this.data.sendcode) {
      return
    }
    this.setData({
      sendcode: true
    })
    var _this=this;
    var currentTime=_this.data.currentTime;
    sms({ phone: this.data.phone, template: 'reg'}).then(res=>{
      console.log(res)
      wx.showToast({
        title: res.data.msg,
        duration: 1000,
        icon: 'none'
      })
      if(res.data.code==666){
       let interval=setInterval(function(){
         currentTime--;
          _this.setData({
            time: currentTime+'秒',
            disabled:true
          })
         if (currentTime<=0){
            clearInterval(interval)
           app.globalData.regtime = ""
            _this.setData({
              time:'重新发送',
              currentTime:61,
              disabled:false,
              sendcode:false
            })
          }
        },1000)
      } else {
        this.setData({
          sendcode:false
        })
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },
  register() {
    var that =this
    let avatarUrl = app.globalData.userInfo.avatarUrl;
    let nickName = app.globalData.userInfo.nickName;
    register({ phone: this.data.phone, password: this.data.password,phone_code:this.data.code,avatarUrl,nickName}).then(res => {
     if(res.data.code==666){
        if(res.data.data.token){
          app.globalData.token=res.data.data.token,
          app.globalData.business_id = res.data.data.data.id
          app.globalData.is_identify = res.data.data.data.is_identify
          app.globalData.is_auth = res.data.data.data.is_auth,
            app.globalData.is_bank_auth = res.data.data.data.is_bank_auth
          app.globalData.guojia = res.data.data.data.business_country ? res.data.data.data.business_country : '';
          wx.setStorageSync('phone', that.data.phone)
          wx.setStorageSync('password', that.data.password)
          wx.setStorageSync('token', res.data.data.token)
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
          setTimeout(()=>{
            wx.switchTab({
              url: '../../../pages/fullStock/fullStock',
            })
          },1000)
        }
      }else{
       wx.showToast({
         title: res.data.msg,
         icon: 'none'
       })
      }
    })
  },
 
  syncData(e) {
    let { key } = e.target.dataset;
    let val = e.detail.value;
    this.setData({
      [key]: val,
      colora:true
    })
  },
  syncDatb(e) {
    let { key } = e.target.dataset;
    let val = e.detail.value;
    this.setData({
      [key]: val,
      colorb:true
    })
    console.log('key', e)
  },
   syncDatc(e) {
    let { key } = e.target.dataset;
    let val = e.detail.value;
    this.setData({
      [key]: val,
      colorc:true
    })
    console.log('key', e)
  }

})